<template>
  <div class="home-container">
    <el-container>
      <el-header>
        <div class="header-left-container">
          <img src="~img/heima.png" alt="">
          <span>电商后台管理系统</span>
        </div>
        <div>
          <el-dropdown @command="handleCommand">
            <el-button>
              {{$store.getters.userInfo.username}}<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-header>

      <el-container>
        <el-aside :width="isCollapse?'64px':'200px'">
          <div class="top-btn" @click="topButtonClick">|||</div>
          <el-menu
            class="el-menu-vertical-demo"
            background-color="#383f4a"
            text-color="#fff"
            active-text-color="#ffd04b"
            unique-opened
            :collapse-transition="false"
            :router="true"
            :default-active="routePath"
            :collapse="isCollapse"
          >
            <el-submenu :index="menu.id+''" v-for="(menu,index) in $store.getters.leftMenus" :key="menu.id">
              <template slot="title">
                <i :class="'icon iconfont '+menuIcons[index]"></i>
                <span class="menu-name">{{menu.name}}</span>
              </template>
              <el-menu-item-group>
                <el-menu-item :index="'/'+ child.path" v-for="child in menu.children" :key="child.id">
                  {{child.name}}
                </el-menu-item>
              </el-menu-item-group>
            </el-submenu>
          </el-menu>

        </el-aside>
        <el-main>
          <router-view/>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>

  import {leftMenus,userInfo} from "network/homeRequest/HomeRequest";
  import * as methods from 'store/methods.const';

  // import UserInfoMixin from 'mixin/UserInfoMixin/userInfoMixin'

  export default {
    name: "Home",
    // mixins:[UserInfoMixin],
    data:function () {

      return {
        isCollapse:false,
        menuIcons:[
          'iconguanli',
          'iconlogooo_huabanfuben',
          'iconpermission',
          'iconuser_name',
          'icondianpu'
        ]
      }
    },
    computed:{

      routePath(){

        return this.$route.path;
      },
      // leftMenus(){
      //
      //   this.$store.dispatch(methods.GET_Left_Menu);
      //
      // }
    },
    mounted:function () {

      // this.loadLeftMenusData();
    },
    destroyed:function(){

      // console.log('home destroyed');
    },
    methods:{

      logoutClick(){

          sessionStorage.removeItem('token');
          this.$router.replace('/loginRegister');
      },
      loadLeftMenusData(){

        leftMenus().then(data=>{

          this.leftMenus = data;
          this.$store.commit(methods.SAVE_LEFT_MENU,data);
        });
      },
      handleCommand(command){

        if (command === 'logout'){

          this.logoutClick();
        }
      },
      topButtonClick(){

        this.isCollapse = !this.isCollapse;
      }
    }
  }
</script>

<style lang="less" scoped>

  .home-container{
    height: 100%;
  }
  .el-container{
    height: 100%;
  }
  .el-header{
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #404547;
    padding-left: 0px;
    color: white;
    .header-left-container{
      display: flex;
      align-items: center;
      span{
        margin-left: 10px;
      }
    }
  }
  .el-aside{
    background: #383f4a;
  }

  .el-main{
    background: #fff;
  }

  .el-menu{
    border-right: none;
  }
  .menu-name{

    margin-left: 10px;
  }

  .top-btn{
    text-align: center;
    color: #ccc;
    font-size: 16px;
    padding: 10px 0px;
    letter-spacing: 3px;
    cursor: pointer;
  }

  .top-btn:hover{
    background: #272c34;
  }
</style>
